package com.example.androidcaculator.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.androidcaculator.R;
import com.example.androidcaculator.utils.Converter;

import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;

public class ConverterLengthActivity extends AppCompatActivity {

    private Button[] btn = new Button[10];
    private Button point;
    private Button clear, back;

    private Spinner lengthUnit1;
    private Spinner lengthUtil2;
    private TextView num1, num2;


    private void initView() {

        //绑定界面元素
        btn[0] = findViewById(R.id.length_0);
        btn[1] = findViewById(R.id.length_1);
        btn[2] = findViewById(R.id.length_2);
        btn[3] = findViewById(R.id.length_3);
        btn[4] = findViewById(R.id.length_4);
        btn[5] = findViewById(R.id.length_5);
        btn[6] = findViewById(R.id.length_6);
        btn[7] = findViewById(R.id.length_7);
        btn[8] = findViewById(R.id.length_8);
        btn[9] = findViewById(R.id.length_9);

        point = findViewById(R.id.length_point);
        clear = findViewById(R.id.lengthClear);
        back = findViewById(R.id.lengthBack);

        num1 = findViewById(R.id.lengthNum1);
        num2 = findViewById(R.id.lengthNum2);

        //为按钮绑定监听器
        btn[0].setOnClickListener(actionNum);
        btn[1].setOnClickListener(actionNum);
        btn[2].setOnClickListener(actionNum);
        btn[3].setOnClickListener(actionNum);
        btn[4].setOnClickListener(actionNum);
        btn[5].setOnClickListener(actionNum);
        btn[6].setOnClickListener(actionNum);
        btn[7].setOnClickListener(actionNum);
        btn[8].setOnClickListener(actionNum);
        btn[9].setOnClickListener(actionNum);

        point.setOnClickListener(actionNum);
        clear.setOnClickListener(actionOperation);
        back.setOnClickListener(actionOperation);

    }

    private String input = new String();
    //输入数字时
    private View.OnClickListener actionNum = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String context = ((Button) view).getText().toString();
            String valueNum1 = num1.getText().toString();
            Converter converter = new Converter();
            String unit1 = lengthUnit1.getSelectedItem().toString();
            String unit2 = lengthUtil2.getSelectedItem().toString();
            String valueNum2 = new String();

            if (".".equals(context)) {
                //输入小数点
                if (valueNum1.equals("0") && input.equals("")) {
                    //当输入
                    input += "0.";

                } else {
                    input += ".";
                }
                num1.setText(input);
            } else {
                if ("0".equals(valueNum1))
                    input = context;
                else
                    input += context;
                num1.setText(input);
                valueNum2 = converter.LengthConversion(input, unit1, unit2);
                num2.setText(valueNum2);

            }

        }
    };


    private View.OnClickListener actionOperation = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String context = ((Button) view).getText().toString();
            String valueNum1 = num1.getText().toString();
            String valueNum2 = num2.getText().toString();
            Converter converter = new Converter();
            String unit1 = lengthUnit1.getSelectedItem().toString();
            String unit2 = lengthUtil2.getSelectedItem().toString();

            if (context.equals("CE")) {
                valueNum1 = "0";
                num1.setText(valueNum1);
                num2.setText("0");
                input = "";

            } else if ("Back".equals(context)) {
                if (valueNum1.length() > 1) {
                    input = input.substring(0, input.length() - 1);
                    num1.setText(input);
                    converter.LengthConversion(input, unit1, unit2);
                } else {
                    num1.setText("0");
                    input = "";
                    num2.setText("0");
                }


            }


        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_converter_length);

        //下拉框显示列表
        lengthUnit1 = findViewById(R.id.lengthUnit1);
        lengthUtil2 = findViewById(R.id.lengthUnit2);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.unit1, android.R.layout.simple_spinner_item);
        lengthUnit1.setAdapter(adapter);
        adapter = ArrayAdapter.createFromResource(this, R.array.unit2, android.R.layout.simple_spinner_item);
        lengthUtil2.setAdapter(adapter);

        initView();

        //用户选择不同的单位时再刷新一遍数据
        lengthUnit1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String valueNum1 = num1.getText().toString();
                String unit1 = lengthUnit1.getSelectedItem().toString();
                String unit2 = lengthUtil2.getSelectedItem().toString();
                Converter converter = new Converter();
                String valueNum2 = converter.LengthConversion(valueNum1,unit1,unit2);
                num2.setText(valueNum2);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        lengthUtil2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String valueNum1 = num1.getText().toString();
                String unit1 = lengthUnit1.getSelectedItem().toString();
                String unit2 = lengthUtil2.getSelectedItem().toString();
                Converter converter = new Converter();
                String valueNum2 = converter.LengthConversion(valueNum1,unit1,unit2);
                num2.setText(valueNum2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.length_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        Intent intent;
        switch (id) {
            case R.id.length_action_common:
                intent = new Intent(ConverterLengthActivity.this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.length_action_scientific:
                intent = new Intent(ConverterLengthActivity.this, ScientificActivity.class);
                startActivity(intent);
                break;
            case R.id.length_transform_currency:
                intent = new Intent(ConverterLengthActivity.this, ConverterCurrencyActivity.class);
                startActivity(intent);
                break;
            case R.id.length_transform_base:
                intent = new Intent(ConverterLengthActivity.this, ConverterBaseActivity.class);
                startActivity(intent);
                break;
            case R.id.length_transform_volume:
                intent = new Intent(ConverterLengthActivity.this, ConverterVolumeActivity.class);
                startActivity(intent);
                break;
            case R.id.length_action_dateCal:
                intent = new Intent(ConverterLengthActivity.this, DateActivity.class);
                startActivity(intent);
                break;
            case R.id.length_action_help:
                AlertDialog.Builder helpTxt = new AlertDialog.Builder(ConverterLengthActivity.this);
                helpTxt.setMessage("这里是帮助文档\n本计算器由Dio开发").setTitle("Help");
                helpTxt.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                helpTxt.show();
                break;
            case R.id.length_action_exit:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra(MainActivity.EXIST, true);
                startActivity(intent);


        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }


        }
        return super.onMenuOpened(featureId, menu);
    }


}